/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package corejavapractice3;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author dlao
 */
public class CoreJavaPractice3 {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                CalculatorFrame frame = new CalculatorFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

class CalculatorFrame extends JFrame {

    private static final int DEFAULT_WIDTH = 500;
    private static final int DEFAULT_LENGTH = 600;

    public CalculatorFrame() {
        setSize(DEFAULT_WIDTH, DEFAULT_LENGTH);
        setTitle("Calculator");
        CalculatorComponent component = new CalculatorComponent();
        add(component);
        pack();
    }
}

class CalculatorComponent extends JPanel {

    private JPanel panel;
    private ActionListener insert = new InsertAction();
    private ActionListener command = new CommandAction();
    private JButton display;
    private boolean start;
    public double result;
    private String lastCommand;

    public CalculatorComponent() {
//        panel = new JPanel();
        result = 0;
        lastCommand = "=";
        start = true;
        setLayout(new BorderLayout());
        display = new JButton("0");
        display.setEnabled(false);
        add(display, BorderLayout.NORTH);

        panel = new JPanel();
        panel.setLayout(new GridLayout(4, 4));

        addButton("7", insert);
        addButton("8", insert);
        addButton("9", insert);
        addButton("/", command);


        addButton("4", insert);
        addButton("5", insert);
        addButton("6", insert);
        addButton("*", command);

        addButton("1", insert);
        addButton("2", insert);
        addButton("3", insert);
        addButton("-", command);

        addButton("0", insert);
        addButton(".", insert);
        addButton("=", command);
        addButton("+", command);

        add(panel, BorderLayout.CENTER);

    }

    public void addButton(String label, ActionListener listener) {
        JButton button = new JButton(label);
        button.addActionListener(listener);
        panel.add(button);
    }

    private class InsertAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            if (start) {
                display.setText("");
                start = false;
            } 
                display.setText(display.getText() + event.getActionCommand());    
        }
    }

    private class CommandAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand();

            if (start) {
                if (command.equals("-")) {
                    display.setText(command);
                    start = false;
                } else {
                    lastCommand = command;
                }
            } else {
                System.out.print("first: "+ result + "  "+"display: "+display.getText()+"     ");
                calculate(Double.parseDouble(display.getText()));
                System.out.println("result: "+ result);
                lastCommand = command;
                start = true;
            }
        }
    }

    public void calculate(double x) {
        if (lastCommand.equals("+")) {
            result += x;
        } else if (lastCommand.equals("=")) {
            result = x;
        } else if (lastCommand.equals("*")) {
            result *= x;
        } else if (lastCommand.equals("/")) {
            result /= x;
        }else if (lastCommand.equals("-")) {
            result -= x;
        }
        display.setText("" + result);
    }
}
